﻿var EasyCodeApp = angular.module('easyCodeApp', [
    'ui.router',
    'ui.bootstrap',
    'oc.lazyLoad',
    'ngSanitize',
    'LocalStorageModule',
    'app.core',
   //'angularFileUpload',
   //'ui.calendar',
   //'angucomplete-alt',
   //'datatables',
   //'checklist-model',
   //'jsTree.directive'
]);

/* Configure ocLazyLoader(refer: https://github.com/ocombe/ocLazyLoad) */
EasyCodeApp.config(['$ocLazyLoadProvider', function ($ocLazyLoadProvider) {
    $ocLazyLoadProvider.config({
        cssFilesInsertBefore: 'ng_load_plugins_before' // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
    });
}]);

/* Setup global settings */
EasyCodeApp.factory('settings', ['$rootScope', function ($rootScope) {
    // supported languages
    var settings = {
        layout: {
            pageSidebarClosed: false, // sidebar menu state
            pageBodySolid: false, // solid body color state
            pageAutoScrollOnLoad: 1000 // auto scroll to top on page load
        },
        //layoutImgPath: Metronic.getAssetsPath() + 'admin/layout/img/',
        //layoutCssPath: Metronic.getAssetsPath() + 'admin/layout/css/'
    };

    $rootScope.settings = settings;

    return settings;
}]);

EasyCodeApp.constant('ngAuthSettings', {
    apiServiceBaseUri: '',
    clientId: 'ngAuthApp'
});

/* Setup Rounting For All Pages */
EasyCodeApp.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {

    // Redirect any unmatched url
    $urlRouterProvider.otherwise("/home");

    $stateProvider
    .state('home', {
        url: "/home",
        templateUrl: "/app/views/home/home.html",
        data: { pageTitle: 'Home' },
        controller: "Home",
        controllerAs: 'vm',
        resolve: {
            deps: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([{
                    name: 'easyCodeApp',
                    files: [
                        '/app/views/home/homeCtrl.js'
                    ]
                }]);
            }]
        }
    })

    .state('login', {
        url: "/login",
        templateUrl: "/app/views/login/login.html",
        data: { pageTitle: 'Login' },
        controller: "Login",
        controllerAs: 'vm',
        resolve: {
            deps: ['$ocLazyLoad', function ($ocLazyLoad) {
                return $ocLazyLoad.load([{
                    name: 'easyCodeApp',
                    files: [
                        '/app/views/login/loginCtrl.js'
                    ]
                }]);
            }]
        }
    })
}]);

/* Init global settings and run the app */
EasyCodeApp.run(["$rootScope", "settings", "$state", 'authService', function ($rootScope, settings, $state, authService) {
    $rootScope.$state = $state; // state to be accessed from view
    authService.fillAuthData();
    //if (authService.authentication.isAuth)
    //    $state.go('userDashboard');
    //else
    //    $state.go('login');


    //$rootScope.$on('$stateChangeStart', function (e, to) {
    //    if (to.url != '/login' && !authService.authentication.isAuth) {
    //        e.preventDefault();
    //        // Optionally set option.notify to false if you don't want 
    //        // to retrigger another $stateChangeStart event
    //        $state.go('login', { notify: false });
    //    }
    //});
}]);